package com.toommi.refuse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toommi.refuse.entity.*;
import com.toommi.refuse.mapper.BuildingMapper;
import com.toommi.refuse.mapper.HouseholdMapper;
import com.toommi.refuse.mapper.NeighbourhoodMapper;
import com.toommi.refuse.service.NeighbourhoodService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author yangg
 * @since 2019-07-25 10:10
 */
@Service
@AllArgsConstructor
public class NeighbourhoodServiceImpl implements NeighbourhoodService {

    private NeighbourhoodMapper neighbourhoodMapper;

    private BuildingMapper buildingMapper;

    private HouseholdMapper householdMapper;

    @Override
    public void addNeighbourhood(Neighbourhood neighbourhood) {
        neighbourhood.setCreateTime(System.currentTimeMillis());
        neighbourhood.setUpdateTime(System.currentTimeMillis());
        neighbourhoodMapper.insert(neighbourhood);
    }

    @Override
    public void deleteNeighbourhood(long id) {
        neighbourhoodMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Neighbourhood> findByStreet(int pageNum, int pageSize, String street) {

        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("id");
        Neighbourhood neighbourhood = new Neighbourhood();
        neighbourhood.setStreet(street);

        List<Neighbourhood> neighbourhoodList = neighbourhoodMapper.select(neighbourhood);

        return new PageInfo<>(neighbourhoodList);
    }

    @Override
    public PageInfo<Neighbourhood> findNeighbourhoods(int pageNum, int pageSize, String keyWord) {

        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("id");
        List<Neighbourhood> neighbourhoods = null;
        if (StringUtils.isNotEmpty(keyWord)) {
            NeighbourhoodExample example = new NeighbourhoodExample();

            example.createCriteria().andNameLike("%" + keyWord + "%");
            neighbourhoods = neighbourhoodMapper.selectByExample(example);
        } else {
            neighbourhoods = neighbourhoodMapper.selectAll();
        }

        return new PageInfo<>(neighbourhoods);
    }


    @Override
    public void addBuilding(Building building) {
        building.setCreateTime(System.currentTimeMillis());
        building.setUpdateTime(System.currentTimeMillis());
        buildingMapper.insert(building);
    }

    @Override
    public void deleteBuilding(long id) {
        buildingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Building> findBuildings(int pageNum, int pageSize, long neighbourhood) {

        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("id");
        Building building = new Building();
        building.setNeighbourhood(neighbourhood);

        List<Building> neighbourhoodList = buildingMapper.select(building);

        return new PageInfo<>(neighbourhoodList);

    }

    @Override
    public void addHouseHold(Household household) {
        household.setCode(UUID.randomUUID().toString().replaceAll("-", ""));
        household.setCreateTime(System.currentTimeMillis());
        household.setUpdateTime(System.currentTimeMillis());
        householdMapper.insert(household);
    }

    @Override
    public void deleteHouseHold(long id) {
        householdMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Household> findHouseHolds(int pageNum, int pageSize, long build) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("id");
        Household household = new Household();
        household.setBuilding(build);

        List<Household> householdList = householdMapper.select(household);


        return new PageInfo<>(householdList);
    }

    @Override
    public Household findHouseholdByCode(String code) {

        HouseholdExample example = new HouseholdExample();
        example.createCriteria().andCodeEqualTo(code);

        return householdMapper.selectOneByExample(example);
    }

    /**
     * 后台获取小区信息
     * @param request
     * @return
     */
    @Override
    public Object getNeighbourhoodslist(HttpServletRequest request) {
        Integer page=Integer.valueOf(request.getParameter("page"));
        Integer limit=Integer.valueOf(request.getParameter("limit"));
        PageHelper.startPage(page, limit);
        String name=request.getParameter("name");
        List<Map<String,Object>> sysUserList = neighbourhoodMapper.getNeighbourhoodslist(name);
        Map<String,Object> result=new HashMap<String,Object>();
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(sysUserList);
        //把唯一编码
        result.put("count", pageInfo.getTotal());
        result.put("data", sysUserList);
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }

    /**
     * 获取后端楼栋列表
     * @param request
     * @return
     */
    @Override
    public Object buildlist(HttpServletRequest request) {
        Integer page=Integer.valueOf(request.getParameter("page"));
        Integer limit=Integer.valueOf(request.getParameter("limit"));
        PageHelper.startPage(page, limit);
        String nid=request.getParameter("nid");
        List<Map<String,Object>> sysUserList = neighbourhoodMapper.buildlist(nid);
        Map<String,Object> result=new HashMap<String,Object>();
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(sysUserList);
        //把唯一编码
        result.put("count", pageInfo.getTotal());
        result.put("data", sysUserList);
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }
}
